package server;

import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import ocsf.server.*;

/**
 * An instance of an Observable Server
 * @author Benny
 *
 */
public class ObservableServer extends AbstractServer {

	private mysqlConnection sqlCon;
	//The default port to listen on.
	final public static int DEFAULT_PORT = 5555;
	public UserQueryHandle userQuery;
    public LibrarianQueryHandle librarianQuery;
	public ManagerQueryHandle managerQuery;

	/**
	 * Constructor of the object
	 * @param port port for connection
	 * @param c established connection to the SQL DataBase
	 */
	public ObservableServer(int port, mysqlConnection c) 
	  {
	    super(port); 
	    sqlCon=c;
	    userQuery = new UserQueryHandle(sqlCon);
	    librarianQuery = new LibrarianQueryHandle(sqlCon);
	    managerQuery = new ManagerQueryHandle(sqlCon);
	    
	  }
	
	/**
	 * Handling message from client and sending an answer
	 * @param msg received message from a specific client, through the running server
	 * @param client a specific client connected to the running server
	 */
	public void handleMessageFromClient(Object msg, ConnectionToClient client)
	{
		@SuppressWarnings("unchecked")
		ArrayList<Object> messages=(ArrayList<Object>) msg;
		ArrayList<Object> res = new ArrayList<Object>();		
		ArrayList<Object> res2 = new ArrayList<Object>();
		String role = (String) messages.get(0);
		System.out.println(role);

	  if(messages.get(0).equals("Login"))
	  {
		  try {
			  res = sqlCon.LogIn((int) messages.get(1),(String) messages.get(2));
			  System.out.println(res.toString());
			client.sendToClient(res);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	  }
	  
	  if(messages.get(0).equals("20"))
	  {
		  try {
			  res2 = sqlCon.LogOut((int)(messages.get(1)));
			  System.out.println(res2.toString());
			client.sendToClient(res2);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	  }
	  
	  if(messages.get(0).equals("BookSearch"))
	  {
		  System.out.println("BOOKSEARCH");
		  try 
		  {
			  if(messages.get(1).equals("Book Name"))
			  res = sqlCon.searchBookName(messages.get(2).toString());
			  if(messages.get(1).equals("Author"))
			  res = sqlCon.searchBookAuthor(messages.get(2).toString());
			  if(messages.get(1).equals("Subject"))
			  res = sqlCon.searchBookSubject(messages.get(2).toString());
			  if(messages.get(1).equals("Free Description"))
			  res = sqlCon.searchBookDesc(messages.get(2).toString());
			  
			client.sendToClient(res);
		} catch (IOException e) {
			e.printStackTrace();
		}
	  }
	  
	  if(messages.get(0).equals("SearchCopyID"))
	  {
		  try {
			  res = sqlCon.searchCopyID((int)messages.get(1));
			client.sendToClient(res);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	  }
	  
	  
	  switch (role)
	  {
	  /**
	   * direct the specific message and client to the User Query Controller
	   */
	  case ("1"):
	  {
		  try {
			userQuery.handleMessage((Object)messages, client);
		} catch (SQLException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} 
	  }
	  /**
	   * direct the specific message and client to the Librarian Query Controller
	   */
	  case ("2"):
	  {
		  try {
			librarianQuery.handleMessage(messages, client);
		} catch (SQLException | IOException e) {
			
			e.printStackTrace();
		}
	  }
	  /**
	   * direct the specific message and client to the Manager Query Controller
	   */
	  case ("3"):
	  {
		
		try {
			managerQuery.handleMessage(messages, client);
		} catch (SQLException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	  }  
	  }
	}
	
	protected void serverStarted() {
        //gui.lblStatus.setText("Status: Started");
        System.out.println("Server listening for connections on port "
                        + getPort());
	}

protected void serverStopped() {
        //gui.lblStatus.setText("Status: Stoped");
        System.out.println("Server has stopped listening for connections.");
}
}

